package rps.client.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class Square extends JButton {
	private int row = 0;
	private int column = 0;
	StackedIcon sIcon;
	/**
	 * 
	 * @param row
	 * @param column
	 * @param icon
	 */
	public Square(int row, int column, Icon icon, ActionListener listener) {
		super();
		setIcon(icon);

		this.row = row;
		this.column = column;

		//setEnabled(false);
		setBorder(BorderFactory.createEmptyBorder());
		
		addActionListener(listener);
	}
	
	@Override
	public void setIcon(Icon icon) {
		Icon background;
		if ((row*7+column)%2 != 0) {
			background = Picture.LIGHT_GRASS.getImage();
		} else {
			background = Picture.DARK_GRASS.getImage();
		}
		if (sIcon != null && isPlayer() && isArrow(icon) || icon.equals(Picture.ATTACK.getImage())) {
			sIcon = new StackedIcon (background, sIcon.getKind(), icon);
		} else {
			sIcon = new StackedIcon (background, icon);
		}
		super.setIcon(sIcon);
		super.setDisabledIcon(sIcon);
	}
	
	@Override
	public void setDisabledIcon (Icon icon) {
		super.setDisabledIcon(new StackedIcon (Picture.EMPTY_GRASS.getImage(), icon));
	}
	
	private boolean isArrow (Icon icon) {
		if (icon.equals(Picture.UP.getImage()) || icon.equals(Picture.RIGHT.getImage()) || icon.equals(Picture.DOWN.getImage())|| icon.equals(Picture.LEFT.getImage())) {
			return true;
		}
		
		return false;
	}

	public int getRow() {
		return row;
	}

	public int getColumn() {
		return column;
	}
	
	public boolean isGras () {
		if (sIcon.getKind().equals(Picture.DARK_GRASS.getImage()) || sIcon.equals(Picture.LIGHT_GRASS.getImage())) {
			return true;
		}
		
		return false;
	}
	public boolean isRock () {
		if (sIcon.getKind().equals(Picture.ROCK.getImage()) || sIcon.getKind().equals(Picture.DROCK.getImage())) {
			return true;
		}
		
		return false;
	}
	public boolean isPaper () {
		if (sIcon.getKind().equals(Picture.PAPER.getImage()) || sIcon.getKind().equals(Picture.DPAPER.getImage())) {
			return true;
		}
		
		return false;
	}
	
	public boolean isScissor () {
		if (sIcon.getKind().equals(Picture.SCISSOR.getImage()) || sIcon.getKind().equals(Picture.DSCISSOR.getImage())) {
			return true;
		}
		
		return false;
	}
	
	public boolean isTrap () {
		if (sIcon.getKind().equals(Picture.TRAP.getImage())) {
			return true;
		}
	
		return false;
	}
	
	public boolean isFlag () {
		if (sIcon.getKind().equals(Picture.FLAG.getImage())) {
			return true;
		}
		
		return false;
	}
	
	public boolean isPlayer () {
		if (isScissor() || isRock() || isPaper()) {
			return true;
		}
		
		return false;
	}
}
